package demo.aspect;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Slf4j
@Component
//@Aspect
public class AspectDemo {

//    @Pointcut("execution(* demo.controller.*.*(..))")
//    private void pointCut(){}

    @Pointcut("execution(* demo.controller.*.*(..))")
    public void pointCut(){}


    @Before("pointCut()")
    public void doBefore(){
        log.info("do before...");
    }

    @After("pointCut()")
    public void doAfter(){
        log.info("do after...");
    }

//    @Around("pointCut()")
//    public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
//        log.info("do around before...");
//        Object result = joinPoint.proceed();  //t1
//        log.info("do around after...");
//        return result;
//    }

    @Around("pointCut()")
    public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
        log.info("do around before...");
        Object result = null;
        try{
            result = joinPoint.proceed();
        }catch (Exception e){
            log.error("do around执行目标函数，内部发生异常...");
        }
        log.info("do around after...");
        return result;
    }

    @AfterReturning("pointCut()")
    public void doAfterReturn(){
        log.info("do after returning...");
    }

    @AfterThrowing("execution(* demo.controller.*.*(..))")
    public void doAfterThrowing(){
        log.info("do after throwing...");
    }
}
